package com.perfma.xtest.request;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.compress.utils.Lists;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: junhui.si
 * @date: 2024-04-28 10:31
 **/
public class RequestJson {
    public static void main(String[] args) {
        String filePath = "/Users/sijunhui/Downloads/log/shiwu.txt";
        String fileContent = readFileToString(filePath);
        JSONObject root = JSON.parseObject(fileContent);
        JSONArray objects = root.getJSONArray("object");
        List<String> list = Lists.newArrayList();
        for (int i = 0; i < objects.size(); i++) {
            JSONObject object = objects.getJSONObject(i);
            JSONObject name = object.getJSONObject("name");
            list.add(name.getString("value"));
        }
        list = list.stream().sorted(Comparator.comparing(s -> s)).collect(Collectors.toList());
        list.forEach(System.out::println);
    }

    private static String readFileToString(String filePath) {
        StringBuilder contentBuilder = new StringBuilder();

        try (BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get(filePath)), StandardCharsets.UTF_8))) {
            String line;
            while ((line = br.readLine()) != null) {
                contentBuilder.append(line);
                contentBuilder.append(System.lineSeparator());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return contentBuilder.toString();
    }
}
